<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Learning from Examples &raquo; Flip Coins | Taskflow QuickStart</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,400i,600,600i%7CSource+Code+Pro:400,400i,600" />
  <link rel="stylesheet" href="m-dark+documentation.compiled.css" />
  <link rel="icon" href="favicon.ico" type="image/vnd.microsoft.icon" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta name="theme-color" content="#22272e" />
</head>
<body>
<header><nav id="navigation">
  <div class="m-container">
    <div class="m-row">
      <span id="m-navbar-brand" class="m-col-t-8 m-col-m-none m-left-m">
        <a href="https://taskflow.github.io"><img src="taskflow_logo.png" alt="" />Taskflow</a> <span class="m-breadcrumb">|</span> <a href="index.html" class="m-thin">QuickStart</a>
      </span>
      <div class="m-col-t-4 m-hide-m m-text-right m-nopadr">
        <a href="#search" class="m-doc-search-icon" title="Search" onclick="return showSearch()"><svg style="height: 0.9rem;" viewBox="0 0 16 16">
          <path id="m-doc-search-icon-path" d="m6 0c-3.31 0-6 2.69-6 6 0 3.31 2.69 6 6 6 1.49 0 2.85-0.541 3.89-1.44-0.0164 0.338 0.147 0.759 0.5 1.15l3.22 3.79c0.552 0.614 1.45 0.665 2 0.115 0.55-0.55 0.499-1.45-0.115-2l-3.79-3.22c-0.392-0.353-0.812-0.515-1.15-0.5 0.895-1.05 1.44-2.41 1.44-3.89 0-3.31-2.69-6-6-6zm0 1.56a4.44 4.44 0 0 1 4.44 4.44 4.44 4.44 0 0 1-4.44 4.44 4.44 4.44 0 0 1-4.44-4.44 4.44 4.44 0 0 1 4.44-4.44z"/>
        </svg></a>
        <a id="m-navbar-show" href="#navigation" title="Show navigation"></a>
        <a id="m-navbar-hide" href="#" title="Hide navigation"></a>
      </div>
      <div id="m-navbar-collapse" class="m-col-t-12 m-show-m m-col-m-none m-right-m">
        <div class="m-row">
          <ol class="m-col-t-6 m-col-m-none">
            <li><a href="pages.html">Handbook</a></li>
            <li><a href="namespaces.html">Namespaces</a></li>
          </ol>
          <ol class="m-col-t-6 m-col-m-none" start="3">
            <li><a href="annotated.html">Classes</a></li>
            <li><a href="files.html">Files</a></li>
            <li class="m-show-m"><a href="#search" class="m-doc-search-icon" title="Search" onclick="return showSearch()"><svg style="height: 0.9rem;" viewBox="0 0 16 16">
              <use href="#m-doc-search-icon-path" />
            </svg></a></li>
          </ol>
        </div>
      </div>
    </div>
  </div>
</nav></header>
<main><article>
  <div class="m-container m-container-inflatable">
    <div class="m-row">
      <div class="m-col-l-10 m-push-l-1">
        <h1>
          <span class="m-breadcrumb"><a href="Examples.html">Learning from Examples</a> &raquo;</span>
          Flip Coins
        </h1>
        <nav class="m-block m-default">
          <h3>Contents</h3>
          <ul>
            <li><a href="#FlipCoinsProblemFormulation">Problem Formulation</a></li>
            <li><a href="#FlipCoinsProbabilistic">Probabilistic Conditions</a></li>
            <li><a href="#FlipCoinsTernaryCoins">Ternary Coins</a></li>
          </ul>
        </nav>
<p>We study dynamic control flow of non-determinism using conditional tasking. Non-deterministic control flow is a fundamental building block in many optimization and simulation algorithms that rely on stochastic convergence rules or probabilistic pruning.</p><section id="FlipCoinsProblemFormulation"><h2><a href="#FlipCoinsProblemFormulation">Problem Formulation</a></h2><p>We have a fair binary coin and want to simulate its tosses. We flip the coin for five times. Apparently, the probability for the result to be all heads is 1/32. It is equivalently to say the expected number we need to toss for obtaining five heads is 32.</p><div class="m-graph"><svg style="width: 54.800rem; height: 4.400rem;" viewBox="0.00 0.00 547.58 44.00">
<g transform="scale(1 1) rotate(0) translate(4 40)">
<title>G</title>
<g class="m-node m-flat">
<title>a</title>
<ellipse cx="29.43" cy="-18" rx="29.37" ry="18"/>
<text text-anchor="middle" x="29.43" y="-15.5" font-family="Helvetica,sans-Serif" font-size="10.00">1/2 (H)</text>
</g>
<g class="m-node m-flat">
<title>b</title>
<ellipse cx="128.42" cy="-18" rx="33.61" ry="18"/>
<text text-anchor="middle" x="128.42" y="-15.5" font-family="Helvetica,sans-Serif" font-size="10.00">1/4 (HH)</text>
</g>
<g class="m-edge">
<title>a&#45;&gt;b</title>
<path d="M59.01,-18C66.95,-18 75.76,-18 84.34,-18"/>
<polygon points="84.62,-21.5 94.62,-18 84.62,-14.5 84.62,-21.5"/>
</g>
<g class="m-node m-flat">
<title>c</title>
<ellipse cx="236.24" cy="-18" rx="38.03" ry="18"/>
<text text-anchor="middle" x="236.24" y="-15.5" font-family="Helvetica,sans-Serif" font-size="10.00">1/8 (HHH)</text>
</g>
<g class="m-edge">
<title>b&#45;&gt;c</title>
<path d="M162.03,-18C170.15,-18 179.02,-18 187.7,-18"/>
<polygon points="187.73,-21.5 197.73,-18 187.73,-14.5 187.73,-21.5"/>
</g>
<g class="m-node m-flat">
<title>d</title>
<ellipse cx="356.42" cy="-18" rx="45.83" ry="18"/>
<text text-anchor="middle" x="356.42" y="-15.5" font-family="Helvetica,sans-Serif" font-size="10.00">1/16 (HHHH)</text>
</g>
<g class="m-edge">
<title>c&#45;&gt;d</title>
<path d="M274.63,-18C282.79,-18 291.6,-18 300.29,-18"/>
<polygon points="300.4,-21.5 310.4,-18 300.4,-14.5 300.4,-21.5"/>
</g>
<g class="m-node m-flat">
<title>e</title>
<ellipse cx="488.96" cy="-18" rx="50.75" ry="18"/>
<text text-anchor="middle" x="488.96" y="-15.5" font-family="Helvetica,sans-Serif" font-size="10.00">1/32 (HHHHH)</text>
</g>
<g class="m-edge">
<title>d&#45;&gt;e</title>
<path d="M402.34,-18C410.61,-18 419.36,-18 427.99,-18"/>
<polygon points="428.03,-21.5 438.03,-18 428.03,-14.5 428.03,-21.5"/>
</g>
</g>
</svg>
</div></section><section id="FlipCoinsProbabilistic"><h2><a href="#FlipCoinsProbabilistic">Probabilistic Conditions</a></h2><p>We use condition tasks to simulate the five coin tosses. We create five condition tasks each returning a random binary number. If the return is zero (head toss), the execution moves to the next condition task; or it (tail toss) goes back to the first condition task to start over the simulation.</p><pre class="m-code"><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;taskflow/taskflow.hpp&gt;</span><span class="cp"></span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>

<span class="w">  </span><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">taskflow</span><span class="p">;</span><span class="w"></span>
<span class="w">  </span><span class="n">tf</span><span class="o">::</span><span class="n">Executor</span><span class="w"> </span><span class="n">executor</span><span class="p">;</span><span class="w"></span>
<span class="w">  </span>
<span class="w">  </span><span class="kt">size_t</span><span class="w"> </span><span class="n">rounds</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">10000</span><span class="p">;</span><span class="w"></span>
<span class="w">  </span><span class="kt">size_t</span><span class="w"> </span><span class="n">tosses</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span>
<span class="w">  </span><span class="kt">size_t</span><span class="w"> </span><span class="n">total_tosses</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span>
<span class="w">  </span><span class="kt">double</span><span class="w"> </span><span class="n">average_tosses</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0.0</span><span class="p">;</span><span class="w"> </span>
<span class="w">  </span>
<span class="w">  </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">tosses</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">                       </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;init&quot;</span><span class="p">);</span><span class="w"> </span>
<span class="w">  </span>
<span class="w">  </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="o">++</span><span class="n">tosses</span><span class="p">;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">                       </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;flip-coin-1&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">C</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">                       </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;flip-coin-2&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">D</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">                       </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;flip-coin-3&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">E</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">                       </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;flip-coin-4&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">F</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="mi">2</span><span class="p">;</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">                       </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;flip-coin-5&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span>
<span class="w">  </span><span class="c1">// reach the target; record the number of tosses </span>
<span class="w">  </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">total_tosses</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">tosses</span><span class="p">;</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">                       </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;stop&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span>
<span class="w">  </span><span class="n">A</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">B</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span>
<span class="w">  </span><span class="c1">// five probabilistic conditions</span>
<span class="w">  </span><span class="n">B</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">C</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span><span class="n">C</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">D</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span><span class="n">D</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">E</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span><span class="n">E</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">F</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span><span class="n">F</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">G</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span>
<span class="w">  </span><span class="c1">// repeat the flip-coin simulation by rounds times</span>
<span class="w">  </span><span class="n">executor</span><span class="p">.</span><span class="n">run_n</span><span class="p">(</span><span class="n">taskflow</span><span class="p">,</span><span class="w"> </span><span class="n">rounds</span><span class="p">).</span><span class="n">wait</span><span class="p">();</span><span class="w"></span>
<span class="w">  </span>
<span class="w">  </span><span class="c1">// calculate the expected number of tosses</span>
<span class="w">  </span><span class="n">average_tosses</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total_tosses</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="p">(</span><span class="kt">double</span><span class="p">)</span><span class="n">rounds</span><span class="p">;</span><span class="w"></span>
<span class="w">  </span>
<span class="w">  </span><span class="n">assert</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">fabs</span><span class="p">(</span><span class="n">average_tosses</span><span class="mf">-32.0</span><span class="p">)</span><span class="o">&lt;</span><span class="mf">1.0</span><span class="p">);</span><span class="w"></span>
<span class="w">  </span>
<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span></pre><p>Running the taskflow by a fair number of times, the average tosses we have is close to 32. The taskflow diagram is depicted below.</p><div class="m-graph"><svg style="width: 90.900rem; height: 16.000rem;" viewBox="0.00 0.00 909.12 160.00">
<g transform="scale(1 1) rotate(0) translate(4 156)">
<title>Taskflow</title>
<g class="m-node m-flat">
<title>p0x7fa9a4803e30</title>
<ellipse cx="27" cy="-87" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-84.5" font-family="Helvetica,sans-Serif" font-size="10.00">init</text>
</g>
<g class="m-node">
<title>p0x7fa9a4803f50</title>
<polygon points="145.11,-105 90.89,-87 145.11,-69 199.34,-87 145.11,-105"/>
<text text-anchor="middle" x="145.11" y="-84.5" font-family="Helvetica,sans-Serif" font-size="10.00">flip&#45;coin&#45;1</text>
</g>
<g class="m-edge">
<title>p0x7fa9a4803e30&#45;&gt;p0x7fa9a4803f50</title>
<path d="M54.06,-87C62.01,-87 71.13,-87 80.45,-87"/>
<polygon points="80.58,-90.5 90.58,-87 80.58,-83.5 80.58,-90.5"/>
</g>
<g class="m-edge">
<title>p0x7fa9a4803f50&#45;&gt;p0x7fa9a4803f50</title>
<path stroke-dasharray="5,2" d="M133.21,-101.42C129.05,-112.17 133.02,-123 145.11,-123 153.24,-123 157.7,-118.11 158.48,-111.64"/>
<polygon points="161.9,-110.82 157.02,-101.42 154.97,-111.81 161.9,-110.82"/>
<text text-anchor="middle" x="145.11" y="-126" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node">
<title>p0x7fa9a4804070</title>
<polygon points="296.34,-152 242.11,-134 296.34,-116 350.56,-134 296.34,-152"/>
<text text-anchor="middle" x="296.34" y="-131.5" font-family="Helvetica,sans-Serif" font-size="10.00">flip&#45;coin&#45;2</text>
</g>
<g class="m-edge">
<title>p0x7fa9a4803f50&#45;&gt;p0x7fa9a4804070</title>
<path stroke-dasharray="5,2" d="M161,-99.84C174.61,-110.7 195.84,-125.5 217.22,-132 224.71,-134.27 232.78,-135.62 240.8,-136.35"/>
<polygon points="240.69,-139.85 250.88,-136.96 241.12,-132.86 240.69,-139.85"/>
<text text-anchor="middle" x="220.72" y="-136" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-edge">
<title>p0x7fa9a4804070&#45;&gt;p0x7fa9a4803f50</title>
<path stroke-dasharray="5,2" d="M267.89,-125.35C244.06,-117.85 209.37,-106.92 183.1,-98.65"/>
<polygon points="184.07,-95.29 173.48,-95.62 181.97,-101.96 184.07,-95.29"/>
<text text-anchor="middle" x="220.72" y="-113" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node">
<title>p0x7fa9a4804190</title>
<polygon points="447.56,-105 393.34,-87 447.56,-69 501.79,-87 447.56,-105"/>
<text text-anchor="middle" x="447.56" y="-84.5" font-family="Helvetica,sans-Serif" font-size="10.00">flip&#45;coin&#45;3</text>
</g>
<g class="m-edge">
<title>p0x7fa9a4804070&#45;&gt;p0x7fa9a4804190</title>
<path stroke-dasharray="5,2" d="M324.71,-125.38C348.51,-117.88 383.2,-106.96 409.49,-98.68"/>
<polygon points="410.63,-101.99 419.11,-95.65 408.52,-95.31 410.63,-101.99"/>
<text text-anchor="middle" x="371.95" y="-113" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-edge">
<title>p0x7fa9a4804190&#45;&gt;p0x7fa9a4803f50</title>
<path stroke-dasharray="5,2" d="M393.44,-87C342.12,-87 264.34,-87 209.73,-87"/>
<polygon points="209.48,-83.5 199.48,-87 209.48,-90.5 209.48,-83.5"/>
<text text-anchor="middle" x="296.34" y="-90" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node">
<title>p0x7fa9a48042b0</title>
<polygon points="598.79,-66 544.56,-48 598.79,-30 653.01,-48 598.79,-66"/>
<text text-anchor="middle" x="598.79" y="-45.5" font-family="Helvetica,sans-Serif" font-size="10.00">flip&#45;coin&#45;4</text>
</g>
<g class="m-edge">
<title>p0x7fa9a4804190&#45;&gt;p0x7fa9a48042b0</title>
<path stroke-dasharray="5,2" d="M478.4,-79.2C501.27,-73.22 533.06,-64.92 558.08,-58.38"/>
<polygon points="559.21,-61.7 568,-55.78 557.44,-54.93 559.21,-61.7"/>
<text text-anchor="middle" x="523.17" y="-70" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-edge">
<title>p0x7fa9a48042b0&#45;&gt;p0x7fa9a4803f50</title>
<path stroke-dasharray="5,2" d="M550.67,-50.1C509.22,-52.1 447.27,-55.48 393.45,-60 324.53,-65.78 245.07,-74.89 195.21,-80.91"/>
<polygon points="194.54,-77.46 185.04,-82.14 195.39,-84.41 194.54,-77.46"/>
<text text-anchor="middle" x="371.95" y="-65" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node">
<title>p0x7fa9a48043d0</title>
<polygon points="750.01,-36 695.79,-18 750.01,0 804.23,-18 750.01,-36"/>
<text text-anchor="middle" x="750.01" y="-15.5" font-family="Helvetica,sans-Serif" font-size="10.00">flip&#45;coin&#45;5</text>
</g>
<g class="m-edge">
<title>p0x7fa9a48042b0&#45;&gt;p0x7fa9a48043d0</title>
<path stroke-dasharray="5,2" d="M633.27,-41.27C654.68,-36.96 682.66,-31.34 705.74,-26.7"/>
<polygon points="706.52,-30.11 715.63,-24.71 705.14,-23.25 706.52,-30.11"/>
<text text-anchor="middle" x="674.4" y="-36" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-edge">
<title>p0x7fa9a48043d0&#45;&gt;p0x7fa9a4803f50</title>
<path stroke-dasharray="5,2" d="M713.55,-12.04C683.38,-7.49 638.87,-2 599.79,-2 295.34,-2 295.34,-2 295.34,-2 243.51,-2 193.26,-41.2 166.34,-66.35"/>
<polygon points="163.86,-63.89 159.07,-73.34 168.71,-68.93 163.86,-63.89"/>
<text text-anchor="middle" x="447.56" y="-5" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-node m-flat">
<title>p0x7fa9a48044f0</title>
<ellipse cx="874.12" cy="-18" rx="27" ry="18"/>
<text text-anchor="middle" x="874.12" y="-15.5" font-family="Helvetica,sans-Serif" font-size="10.00">stop</text>
</g>
<g class="m-edge">
<title>p0x7fa9a48043d0&#45;&gt;p0x7fa9a48044f0</title>
<path stroke-dasharray="5,2" d="M804.21,-18C815.19,-18 826.5,-18 836.71,-18"/>
<polygon points="836.89,-21.5 846.89,-18 836.89,-14.5 836.89,-21.5"/>
<text text-anchor="middle" x="825.62" y="-21" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
</g>
</svg>
</div><p>Although the execution of this taskflow is non-deterministic, its control flow can expand to a tree of tasks based on our scheduling rule for conditional tasking (see <a href="ConditionalTasking.html" class="m-doc">Conditional Tasking</a>). Each path from the root to a leaf represents a result of five heads, and none of them can overlap at the same time (no task race). You must follow the same rule when creating a probabilistic framework using conditional tasking.</p></section><section id="FlipCoinsTernaryCoins"><h2><a href="#FlipCoinsTernaryCoins">Ternary Coins</a></h2><p>We can extend the binary coin example to a ternary case. Each condition task has one successor going back to the beginning and two successors moving to the next task. The expected number of tosses to reach five identical results is 3*3*3*3*3 = 243.</p><pre class="m-code"><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">(</span><span class="w"> </span><span class="p">[</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">tosses</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">)</span><span class="w"></span>
<span class="w">                     </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;init&quot;</span><span class="p">);</span><span class="w"> </span>

<span class="c1">// start over the flip again</span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">B</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="o">++</span><span class="n">tosses</span><span class="p">;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">                     </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;flip-coin-1&quot;</span><span class="p">);</span><span class="w"></span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">C</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">                     </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;flip-coin-2&quot;</span><span class="p">);</span><span class="w"></span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">D</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">                     </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;flip-coin-3&quot;</span><span class="p">);</span><span class="w"></span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">E</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">                     </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;flip-coin-4&quot;</span><span class="p">);</span><span class="w"></span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">F</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">rand</span><span class="p">()</span><span class="o">%</span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">                     </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;flip-coin-5&quot;</span><span class="p">);</span><span class="w"></span>

<span class="c1">// reach the target; record the number of tosses </span>
<span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">total_tosses</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">tosses</span><span class="p">;</span><span class="w"> </span><span class="p">})</span><span class="w"></span>
<span class="w">                     </span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;stop&quot;</span><span class="p">);</span><span class="w"></span>

<span class="n">A</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">B</span><span class="p">);</span><span class="w"></span>

<span class="c1">// five probabilistic conditions</span>
<span class="n">B</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">C</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">);</span><span class="w"></span>
<span class="n">C</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">D</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">);</span><span class="w"></span>
<span class="n">D</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">E</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">);</span><span class="w"></span>
<span class="n">E</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">F</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">);</span><span class="w"></span>
<span class="n">F</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">G</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">,</span><span class="w"> </span><span class="n">B</span><span class="p">);</span><span class="w"></span>

<span class="c1">// repeat the flip-coin simulation by rounds times</span>
<span class="n">executor</span><span class="p">.</span><span class="n">run_n</span><span class="p">(</span><span class="n">taskflow</span><span class="p">,</span><span class="w"> </span><span class="n">rounds</span><span class="p">).</span><span class="n">wait</span><span class="p">();</span><span class="w"></span>

<span class="c1">// calculate the expected number of tosses</span>
<span class="n">average_tosses</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">total_tosses</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="p">(</span><span class="kt">double</span><span class="p">)</span><span class="n">rounds</span><span class="p">;</span><span class="w"></span>

<span class="n">assert</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">fabs</span><span class="p">(</span><span class="n">average_tosses</span><span class="mf">-243.0</span><span class="p">)</span><span class="o">&lt;</span><span class="mf">1.0</span><span class="p">);</span><span class="w"></span></pre><div class="m-graph"><svg style="width: 90.900rem; height: 29.700rem;" viewBox="0.00 0.00 909.12 297.00">
<g transform="scale(1 1) rotate(0) translate(4 293)">
<title>Taskflow</title>
<g class="m-node m-flat">
<title>p0x7fca00803e30</title>
<ellipse cx="27" cy="-170" rx="27" ry="18"/>
<text text-anchor="middle" x="27" y="-167.5" font-family="Helvetica,sans-Serif" font-size="10.00">init</text>
</g>
<g class="m-node">
<title>p0x7fca00803f50</title>
<polygon points="145.11,-188 90.89,-170 145.11,-152 199.34,-170 145.11,-188"/>
<text text-anchor="middle" x="145.11" y="-167.5" font-family="Helvetica,sans-Serif" font-size="10.00">flip&#45;coin&#45;1</text>
</g>
<g class="m-edge">
<title>p0x7fca00803e30&#45;&gt;p0x7fca00803f50</title>
<path d="M54.06,-170C62.01,-170 71.13,-170 80.45,-170"/>
<polygon points="80.58,-173.5 90.58,-170 80.58,-166.5 80.58,-173.5"/>
</g>
<g class="m-edge">
<title>p0x7fca00803f50&#45;&gt;p0x7fca00803f50</title>
<path stroke-dasharray="5,2" d="M138.84,-186.29C137.33,-196.39 139.42,-206 145.11,-206 148.76,-206 150.93,-202.06 151.62,-196.57"/>
<polygon points="155.11,-196.21 151.39,-186.29 148.12,-196.37 155.11,-196.21"/>
<text text-anchor="middle" x="145.11" y="-209" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-edge">
<title>p0x7fca00803f50&#45;&gt;p0x7fca00803f50</title>
<path stroke-dasharray="5,2" d="M135.42,-184.93C128.1,-202.9 131.33,-224 145.11,-224 156.42,-224 160.62,-209.8 157.73,-194.78"/>
<polygon points="161,-193.52 154.8,-184.93 154.29,-195.51 161,-193.52"/>
<text text-anchor="middle" x="145.11" y="-227" font-family="Helvetica,sans-Serif" font-size="10.00">2</text>
</g>
<g class="m-node">
<title>p0x7fca00804070</title>
<polygon points="296.34,-282 242.11,-264 296.34,-246 350.56,-264 296.34,-282"/>
<text text-anchor="middle" x="296.34" y="-261.5" font-family="Helvetica,sans-Serif" font-size="10.00">flip&#45;coin&#45;2</text>
</g>
<g class="m-edge">
<title>p0x7fca00803f50&#45;&gt;p0x7fca00804070</title>
<path stroke-dasharray="5,2" d="M151.43,-186.29C160.48,-210.95 181.44,-256.92 217.22,-276 231.29,-283.5 248.68,-281.72 263.48,-277.54"/>
<polygon points="264.82,-280.78 273.23,-274.35 262.64,-274.13 264.82,-280.78"/>
<text text-anchor="middle" x="220.72" y="-281" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-edge">
<title>p0x7fca00804070&#45;&gt;p0x7fca00803f50</title>
<path stroke-dasharray="5,2" d="M259.56,-258.01C245.69,-254.67 230.09,-249.59 217.22,-242 195.58,-229.23 175.92,-208.24 162.78,-192.22"/>
<polygon points="165.5,-190.01 156.53,-184.36 160.02,-194.36 165.5,-190.01"/>
<text text-anchor="middle" x="220.72" y="-248" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-edge">
<title>p0x7fca00804070&#45;&gt;p0x7fca00803f50</title>
<path stroke-dasharray="5,2" d="M276.07,-252.59C265.95,-246.54 253.37,-238.95 242.22,-232 218.52,-217.22 191.83,-199.95 172.61,-187.4"/>
<polygon points="174.36,-184.37 164.08,-181.82 170.53,-190.22 174.36,-184.37"/>
<text text-anchor="middle" x="220.72" y="-223" font-family="Helvetica,sans-Serif" font-size="10.00">2</text>
</g>
<g class="m-node">
<title>p0x7fca00804190</title>
<polygon points="447.56,-220 393.34,-202 447.56,-184 501.79,-202 447.56,-220"/>
<text text-anchor="middle" x="447.56" y="-199.5" font-family="Helvetica,sans-Serif" font-size="10.00">flip&#45;coin&#45;3</text>
</g>
<g class="m-edge">
<title>p0x7fca00804070&#45;&gt;p0x7fca00804190</title>
<path stroke-dasharray="5,2" d="M320.98,-254.18C345.83,-243.85 385.12,-227.53 413.14,-215.89"/>
<polygon points="414.64,-219.05 422.53,-211.98 411.95,-212.59 414.64,-219.05"/>
<text text-anchor="middle" x="371.95" y="-236" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-edge">
<title>p0x7fca00804190&#45;&gt;p0x7fca00803f50</title>
<path stroke-dasharray="5,2" d="M400.73,-199.53C354.34,-196.72 280.59,-191.4 217.22,-183 208.18,-181.8 198.55,-180.24 189.38,-178.61"/>
<polygon points="189.76,-175.12 179.29,-176.76 188.5,-182.01 189.76,-175.12"/>
<text text-anchor="middle" x="296.34" y="-198" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-edge">
<title>p0x7fca00804190&#45;&gt;p0x7fca00803f50</title>
<path stroke-dasharray="5,2" d="M422.97,-192.14C403.81,-184.68 375.9,-175.07 350.45,-171 299.42,-162.85 240.31,-163.65 198.84,-165.85"/>
<polygon points="198.47,-162.37 188.69,-166.44 198.87,-169.36 198.47,-162.37"/>
<text text-anchor="middle" x="296.34" y="-174" font-family="Helvetica,sans-Serif" font-size="10.00">2</text>
</g>
<g class="m-node">
<title>p0x7fca008042b0</title>
<polygon points="598.79,-131 544.56,-113 598.79,-95 653.01,-113 598.79,-131"/>
<text text-anchor="middle" x="598.79" y="-110.5" font-family="Helvetica,sans-Serif" font-size="10.00">flip&#45;coin&#45;4</text>
</g>
<g class="m-edge">
<title>p0x7fca00804190&#45;&gt;p0x7fca008042b0</title>
<path stroke-dasharray="5,2" d="M467.76,-190.55C493.73,-175.06 540.21,-147.34 569.99,-129.58"/>
<polygon points="571.86,-132.54 578.66,-124.41 568.27,-126.53 571.86,-132.54"/>
<text text-anchor="middle" x="523.17" y="-160" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-edge">
<title>p0x7fca008042b0&#45;&gt;p0x7fca00803f50</title>
<path stroke-dasharray="5,2" d="M558.31,-117.65C489.16,-125.85 341.63,-143.57 217.22,-160 209.49,-161.02 201.29,-162.14 193.31,-163.25"/>
<polygon points="192.79,-159.79 183.37,-164.64 193.76,-166.72 192.79,-159.79"/>
<text text-anchor="middle" x="371.95" y="-143" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-edge">
<title>p0x7fca008042b0&#45;&gt;p0x7fca00803f50</title>
<path stroke-dasharray="5,2" d="M547.94,-111.85C478.53,-111.29 349.27,-114.07 242.22,-138 221.23,-142.69 198.35,-150.21 180.18,-156.78"/>
<polygon points="178.79,-153.56 170.61,-160.31 181.21,-160.13 178.79,-153.56"/>
<text text-anchor="middle" x="371.95" y="-121" font-family="Helvetica,sans-Serif" font-size="10.00">2</text>
</g>
<g class="m-node">
<title>p0x7fca008043d0</title>
<polygon points="750.01,-65 695.79,-47 750.01,-29 804.23,-47 750.01,-65"/>
<text text-anchor="middle" x="750.01" y="-44.5" font-family="Helvetica,sans-Serif" font-size="10.00">flip&#45;coin&#45;5</text>
</g>
<g class="m-edge">
<title>p0x7fca008042b0&#45;&gt;p0x7fca008043d0</title>
<path stroke-dasharray="5,2" d="M622.77,-102.83C647.79,-91.77 687.98,-73.99 716.28,-61.48"/>
<polygon points="718.02,-64.53 725.75,-57.29 715.19,-58.13 718.02,-64.53"/>
<text text-anchor="middle" x="674.4" y="-83" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
<g class="m-edge">
<title>p0x7fca008043d0&#45;&gt;p0x7fca00803f50</title>
<path stroke-dasharray="5,2" d="M695.83,-47C667.34,-47 631.65,-47 599.79,-47 295.34,-47 295.34,-47 295.34,-47 233.7,-47 183.72,-110.26 160.38,-146.05"/>
<polygon points="157.16,-144.6 154.77,-154.92 163.07,-148.34 157.16,-144.6"/>
<text text-anchor="middle" x="447.56" y="-50" font-family="Helvetica,sans-Serif" font-size="10.00">1</text>
</g>
<g class="m-edge">
<title>p0x7fca008043d0&#45;&gt;p0x7fca00803f50</title>
<path stroke-dasharray="5,2" d="M728.4,-36.1C700.19,-22.21 647.59,0 599.79,0 295.34,0 295.34,0 295.34,0 219.42,0 173.07,-96.94 154.92,-144.41"/>
<polygon points="151.55,-143.41 151.38,-154.01 158.12,-145.84 151.55,-143.41"/>
<text text-anchor="middle" x="447.56" y="-3" font-family="Helvetica,sans-Serif" font-size="10.00">2</text>
</g>
<g class="m-node m-flat">
<title>p0x7fca008044f0</title>
<ellipse cx="874.12" cy="-47" rx="27" ry="18"/>
<text text-anchor="middle" x="874.12" y="-44.5" font-family="Helvetica,sans-Serif" font-size="10.00">stop</text>
</g>
<g class="m-edge">
<title>p0x7fca008043d0&#45;&gt;p0x7fca008044f0</title>
<path stroke-dasharray="5,2" d="M804.21,-47C815.19,-47 826.5,-47 836.71,-47"/>
<polygon points="836.89,-50.5 846.89,-47 836.89,-43.5 836.89,-50.5"/>
<text text-anchor="middle" x="825.62" y="-50" font-family="Helvetica,sans-Serif" font-size="10.00">0</text>
</g>
</g>
</svg>
</div><p>Similarly, we can extend the probabilistic condition to any degree.</p></section>
      </div>
    </div>
  </div>
</article></main>
<div class="m-doc-search" id="search">
  <a href="#!" onclick="return hideSearch()"></a>
  <div class="m-container">
    <div class="m-row">
      <div class="m-col-m-8 m-push-m-2">
        <div class="m-doc-search-header m-text m-small">
          <div><span class="m-label m-default">Tab</span> / <span class="m-label m-default">T</span> to search, <span class="m-label m-default">Esc</span> to close</div>
          <div id="search-symbolcount">&hellip;</div>
        </div>
        <div class="m-doc-search-content">
          <form>
            <input type="search" name="q" id="search-input" placeholder="Loading &hellip;" disabled="disabled" autofocus="autofocus" autocomplete="off" spellcheck="false" />
          </form>
          <noscript class="m-text m-danger m-text-center">Unlike everything else in the docs, the search functionality <em>requires</em> JavaScript.</noscript>
          <div id="search-help" class="m-text m-dim m-text-center">
            <p class="m-noindent">Search for symbols, directories, files, pages or
            modules. You can omit any prefix from the symbol or file path; adding a
            <code>:</code> or <code>/</code> suffix lists all members of given symbol or
            directory.</p>
            <p class="m-noindent">Use <span class="m-label m-dim">&darr;</span>
            / <span class="m-label m-dim">&uarr;</span> to navigate through the list,
            <span class="m-label m-dim">Enter</span> to go.
            <span class="m-label m-dim">Tab</span> autocompletes common prefix, you can
            copy a link to the result using <span class="m-label m-dim">⌘</span>
            <span class="m-label m-dim">L</span> while <span class="m-label m-dim">⌘</span>
            <span class="m-label m-dim">M</span> produces a Markdown link.</p>
          </div>
          <div id="search-notfound" class="m-text m-warning m-text-center">Sorry, nothing was found.</div>
          <ul id="search-results"></ul>
        </div>
      </div>
    </div>
  </div>
</div>
<script src="search-v2.js"></script>
<script src="searchdata-v2.js" async="async"></script>
<footer><nav>
  <div class="m-container">
    <div class="m-row">
      <div class="m-col-l-10 m-push-l-1">
        <p>Taskflow handbook is part of the <a href="https://taskflow.github.io">Taskflow project</a>, copyright © <a href="https://tsung-wei-huang.github.io/">Dr. Tsung-Wei Huang</a>, 2018&ndash;2023.<br />Generated by <a href="https://doxygen.org/">Doxygen</a> 1.9.1 and <a href="https://mcss.mosra.cz/">m.css</a>.</p>
      </div>
    </div>
  </div>
</nav></footer>
</body>
</html>
